import requests
import json
import random
import string
from multiprocessing import Pool,Manager,Lock
import multiprocessing

def register(sucess_count,failure_count,lock,num):
    print(num)
    openid = ''.join(random.sample(string.ascii_letters + string.digits, 22))
    postData = {"invite_code":"90683686","openid":openid}
    # data=json.dumps(data)
    r=requests.post('http://www.babei.com/api/v1/login', data = postData)
    code=r.status_code
    print (r.json())
    if r.json()['code']=='10000':
        with lock:
            sucess_count.value+=1
    else:
        lock.acquire()
        failure_count.value += 1
        lock.release()
if __name__=='__main__':
    manager=Manager()
    lock=manager.Lock()
    sucess_count=manager.Value('i',0)
    failure_count=manager.Value('i',0)
    p=Pool()
    print(multiprocessing.cpu_count())
    for i in range(50):
        p.apply_async(register,args=(sucess_count,failure_count,lock,i))
    p.close()
    p.join()
    print ('注册成功次数为：',sucess_count.value)
    print ('注册失败次数为：', failure_count.value)